home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / bestl232 / !best005.c < prev    next >
C/C++ Source or Header  |  1994-04-26  |  5KB  |  160 lines

  1. /*==========================================================================
  2.  *
  3.  *  !BEST005.C                                     Wednesday, April 20, 1994
  4.  *
  5.  *  Linked List Functions
  6.  *  supplementary source file 5 for The BESTLibrary
  7.  *
  8.  *  Authored independently by George Vanous
  9.  *
  10.  *==========================================================================*/
  11.  
  12.  
  13. /* ------------------------------------------------------------------------ */
  14. /* ----------------------------  INCLUDE FILES  --------------------------- */
  15.  
  16. #include <alloc.h>
  17. #include "!bestlib.h"                  // include !BESTLIB.H in compilation
  18.  
  19. /* ------------------------------------------------------------------------ */
  20.  
  21. /*----------------------------------------------------------------------------
  22.  * Search for a node in a linked list.
  23.  *
  24.  * "node" - node to search for
  25.  * "list" - linked list to search through
  26.  *
  27.  * RETURNS:
  28.  * = node of linked list
  29.  * = NULL if node not found in linked list
  30.  */
  31. llist *lst_find(llist *node, llist *list)
  32. {
  33.   while (list != node && list);     // while there is no match and a next node
  34.     list = list->next;              //  hop to next node
  35.  
  36.   return(list);                     // return "node" of linked list, else NULL
  37. }
  38.  
  39. /*----------------------------------------------------------------------------
  40.  * Deallocate all of the nodes of a singly-linked list.
  41.  *
  42.  * "list" - singly linked list to deallocate
  43.  *
  44.  * RETURNS:
  45.  * = NULL
  46.  */
  47. llist_single *lst_free_single(llist_single *list)
  48. {
  49.   llist_single *tail;                  // tail of linked list
  50.  
  51.   while (list->next) {
  52.     /* get to the node that is just before the tail end of the linked list */
  53.     for (tail = list; tail->next->next; tail = tail->next);
  54.     free(tail->next);                  // deallocate tail of linked list
  55.     tail->next = NULL;                 // define new tail of linked list
  56.   }
  57.   free(list);                          // deallocate head of linked list
  58.   return(NULL);
  59. }
  60.  
  61. /*----------------------------------------------------------------------------
  62.  * Deallocate all of the nodes of a doubly-linked list.
  63.  *
  64.  * "list" - doubly linked list to deallocate
  65.  *
  66.  * RETURNS:
  67.  * = NULL
  68.  */
  69. llist_double *lst_free_double(llist_double *list)
  70. {
  71.   llist_double *tail;                  // tail of linked list
  72.  
  73.   tail = (llist_double *)lst_tail( (llist *)list); // get to tail
  74.   while (list->next) {
  75.     tail = tail->prev;                 // go to previous node of linked list
  76.     free(tail->next);                  // deallocate node we just came from
  77.     tail->next = NULL;                 // define new tail of linked list
  78.   }
  79.   free(list);                          // deallocate head of linked list
  80.   return(NULL);
  81. }
  82.  
  83. /*----------------------------------------------------------------------------
  84.  * Return the tail of a linked list.
  85.  *
  86.  * "list" - linked list to return the tail of
  87.  *
  88.  * RETURNS:
  89.  * = tail of linked list
  90.  */
  91. llist *lst_tail(llist *list)
  92. {
  93.   if (!list) return( NULL );           // empty linked list
  94.  
  95.   while (list->next)                   // while there is a next node
  96.     list = list->next;                 //  hop to next node
  97.  
  98.   return(list);                        // return tail of linked list
  99. }
  100.  
  101. /*----------------------------------------------------------------------------
  102.  * Unlink a node from a doubly linked list.
  103.  *
  104.  * "head"   - pointer to first node of linked list
  105.  * "unlink" - node to unlink
  106.  *
  107.  * RETURNS:
  108.  * = next available node
  109.  * = NULL if linked list is empty
  110.  */
  111. llist_double *lst_unlink_double(llist_double **head, llist_double *unlink)
  112. {
  113.   llist_double *next;                  // next node of linked list
  114.  
  115.   next = unlink->next;                 // next node
  116.  
  117.   if (!unlink->prev) {
  118.     /* unlink the first node */
  119.     *head = next;
  120.     if (next) next->prev = NULL;       // new beginning of linked list
  121.   }
  122.   else {
  123.     unlink->prev->next = next;         // unlink node "unlink"
  124.     if (next) next->prev = unlink->prev;
  125.   }
  126.  
  127.   free(unlink);                        // deallocate this node
  128.   return( next );
  129. }
  130.  
  131. /*----------------------------------------------------------------------------
  132.  * Unlink a node from a singly linked list.
  133.  *
  134.  * "head"   - pointer to first node of linked list
  135.  * "unlink" - node to unlink
  136.  *
  137.  * RETURNS:
  138.  * = next available node
  139.  * = NULL if linked list is empty
  140.  */
  141. llist_single *lst_unlink_single(llist_single **head, llist_single *unlink)
  142. {
  143.   llist_single *next, *prev;           // next/previous nodes of linked list
  144.  
  145.   next = unlink->next;                 // next node of linked list
  146.  
  147.   if (*head == unlink) {
  148.     /* unlink the first node */
  149.     *head = next;                      // new beginning of linked list
  150.   }
  151.   else {
  152.     for (prev = *head; prev->next != unlink; prev = prev->next);
  153.     prev->next = next;                 // unlink node "unlink"
  154.   }
  155.  
  156.   free(unlink);                        // deallocate this node
  157.   return( next );
  158. }
  159.  
  160. /*==============================  END-OF-FILE  =============================*/